LASTYEAR=2050;
FIRSTYEAR=1970;

/**
 * calendar的构造函数
 * @year:年份，必须为四位数
 */
function calendar(year,month){
    if(month>12){
        this.month=month-12;
        this.year=year+1;
    }else if(month<1){
        this.month=month+12;
        this.year=year-1
    }else{
        this.year=year;
        this.month=month;
    }
    selectedList=[];
    this.totalDays=getTotalDay(this.year,this.month);
    this.show=false;
    this.initDate;//日历中显示的月份的一号的0：00
    this.dayList=[];
    this.dayListBefore=[];
    this.dayListAfter=[];
    this.renderList=[];
    this.dom=document.createElement('div');

    this.preCal={}

    this.nextCal={}

}
calendar.prototype={
    setInitDate:function(){
       
        var monStr=this.month>9?this.month:'0'+this.month;
        var initDateStr=this.year+'-'+monStr+'-'+'01';
        var dataVal=Date.parse(initDateStr);
        this.initDate=new Date(dataVal);
    },
    render:function(){
        this.setPreCal();
        this.setInitDate();
        this.setNextCal();
        this.setBefore();
        this.setAfter();
        
        


        for(var i=0;i<this.totalDays;i++){
            var tempDay=new dayObj(this.year,this.month,i+1);
            tempDay.cal=this;
            this.dayList[i]=this.dayList[i]?this.dayList[i]:tempDay;
            this.dayList[i].dom.className='day';
            
        }
        this.renderList=this.renderList.concat(this.dayListBefore);
        this.renderList=this.renderList.concat(this.dayList);
        this.renderList=this.renderList.concat(this.dayListAfter);
        for(var i=0;i<this.renderList.length;i++){
            this.dom.appendChild(this.renderList[i].dom);
            if(this.renderList[i].selected){
                this.renderList[i].dom.className+=' selected'
            }
        }

     

    },

    showSelf:function(){
        $('#cal').innerHTML="";
        $('#cal').appendChild(this.dom);
        $('#setYear').value=showCal.year;
        $('#setMonth').value=showCal.month;
        this.show=true;
        this.render();
    },

    setPreCal:function(){
        if(!this.show){
            return;
        }
        var tempCal=new calendar(this.year,this.month-1);
        
        tempCal.nextCal=this;
        this.preCal=tempCal;
   
    },

    setNextCal:function(){

       if(!this.show){
           return;
       }

       var tempCal=new calendar(this.year,this.month+1);
        tempCal.preCal=this;
        this.nextCal=tempCal;

    },
    
    setBefore:function(){
        // this.dayListBefore=[];
        var startDay=this.initDate.getDay();
        
        var preCal=this.preCal?this.preCal:this;

        var preTotal=preCal.totalDays;


        var preStart=preTotal-startDay+1;
        for(var i=startDay;i>0;i--){
            var tempDay=new dayObj(preCal.year,preCal.month,preStart);
            tempDay.dom.className="ODay";
            tempDay.cal=this.preCal;
            tempDay.cal.dayList[tempDay.date-1]=tempDay;
            this.dayListBefore.push(tempDay);
            
            preStart++;
        }
    },
    setAfter:function(){
        // this.dayListAfter=[];
        var endDay=(new Date(this.year+'-'+this.month+'-'+this.totalDays)).getDay();

        var afterTotal=6-endDay;

        var nextCal=this.nextCal?this.nextCal:this;

        for(var i=0;i<afterTotal;i++){
            var tempDay=new dayObj(nextCal.year,nextCal.month,i+1);
            tempDay.dom.className="ODay";
            tempDay.cal=this.nextCal;
            tempDay.cal.dayList[tempDay.date-1]=tempDay;
            this.dayListAfter.push(tempDay)

        }

    }


}






